/*!
 * UEditor
 * version: ueditor
 * build: Wed Aug 10 2016 11:06:16 GMT+0800 (CST)
 */
$(document).ready(function(e){
		var img0,img1,img2,img3;
		var imgpath;
		var list;
	$("#uploads").change(function(e) {
		var imgs=document.getElementById("uploads").files;
		if(imgs.length>4){
			alert("一次不能上传超过4张图片");
			return false;
		}
		// 创建表单对象 
		var formData = new FormData();
			for(var i=0;i<imgs.length;i++){
//				 获取文件名,判断是否是图片 
				if (!/.(gif|jpg|jpeg|png|GIF|JPG|png)$/.test(imgs[i].name)) {
					alert("图片类型必须是.gif,jpeg,jpg,png中的一种");
					return false;
				}
				 //添加文件和id 
				formData.append("img"+i, imgs[i]);
			}
			var urlpath=$("#urlfast").val();
		$.ajax({
			url :"commodity/uploadImgs.action",
			type : 'POST',
			data : formData,
			processData : false,
			contentType : false,
			dataType : "json",
			success : function(data) {
				img0=data.img0;
				img1=data.img1;
				img2=data.img2;
				img3=data.img3;
				list=[img0,img1,img2,img3];
					if(list[0]!=null){
						$("#img0").attr("src","file/detailed/"+list[0]);
						$("#closImg0").css("display","inline");
					}
					if(list[1]!=null){
						$("#img1").attr("src","file/detailed/"+list[1]);	
						$("#closImg1").css("display","inline");
					}
					if(list[2]!=null){
						$("#img2").attr("src","file/detailed/"+list[2]);	
						$("#closImg2").css("display","inline");
					}
					if(list[3]!=null){
						$("#img3").attr("src","file/detailed/"+list[3]);	
						$("#closImg3").css("display","inline");
					}
				$("#submitImg").css("display","inline");
				$("#files").css("display","none");
				$("#imgspan").css("display","none");
					
			},
			error : function(responseStr) { 
				console.log("error");
				} 
			});
});
	/* 确认插入图片按钮*/
	$("#submitImg").click(function(){
		if(list[0]!=null){
insertHtml("<img  src='file/detailed/"+list[0]+"' /><br>");
	$("#img0").attr("src","img/imgDefault.png");
			img0=null;
			$(".defaultImgDiv>button").css("display","none");
		}
		if(list[1]!=null){
			insertHtml("<img  src='file/detailed/"+list[1]+"' /><br>");
			$("#img1").attr("src","img/imgDefault.png");
			img1=null;
			
					}
		if(list[2]!=null){
			insertHtml("<img  src='file/detailed/"+list[2]+"' /><br>");
			$("#img2").attr("src","img/imgDefault.png");
			img2=null;
					}
		if(list[3]!=null){
			insertHtml("<img  src='file/detailed/"+list[3]+"' /><br>");
			$("#img3").attr("src","img/imgDefault.png");
			img3=null;
					}
		
		$("#addImg").css("display","none");
		$("#submitImg").css("display","none");
	});	
//删除将要插入的图片
$("#closImg0").click(function(){
	var src=list[0];
	var val={'imgName':src};
	var url="commodity/deleteImg.action"; 
	$.post(url,val,function(data){
		
	});
	$("#img0").attr("src","img/imgDefault.png");
	$("#closImg0").css("display","none");
	list[0]=null;
	
});
$("#closImg1").click(
		function(){
			var src=list[1];
			var val={'imgName':src};
			var url="commodity/deleteImg.action"; 
			$.post(url,val,function(data){
				
			});
			$("#img1").attr("src","img/imgDefault.png");
			$("#closImg1").css("display","none");
			list[1]=null;
		}
);
$("#closImg2").click(function (){
	var src=list[2];
	var val={'imgName':src};
	var url="commodity/deleteImg.action"; 
	$.post(url,val,function(data){
		
	});
	$("#img2").attr("src","img/imgDefault.png");
	$("#closImg2").css("display","none");
	list[2]=null;
});
$("#closImg3").click(function(){
	var src=list[3];
	var val={'imgName':src};
	var url="commodity/deleteImg.action"; 
	$.post(url,val,function(data){
		
	});
	$("#img3").attr("src","img/imgDefault.png");
	$("#closImg3").css("display","none");
	list[3]=null;
});
$("#closImg").click(function() {
	$("#addImg").css("display", "none");
	for(var i=0;i<4;i++){
		if(list[i]!=null){
			var src=list[i];
			var val={'imgName':src};
			var url="commodity/deleteImg.action"; 
			$.post(url,val,function(data){
				
			});
			$("#img"+i).attr("src","img/imgDefault.png");
			$("#closImg"+i).css("display","none");
			list[i]=null;
		}
	}
	$("#submitImg").css("display","none");
});
});
